package com.xunyun.kotlinwc.main.datasource

import android.annotation.SuppressLint
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.paging.PageKeyedDataSource
import com.xunyun.kotlinwc.main.api.MainService
import com.xunyun.kotlinwc.main.model.Project
import com.xunyun.kotlinwc.network.RetrofitHolder
import com.xunyun.kotlinwc.network.exception.ApiException
import com.xunyun.kotlinwc.network.exception.ErrorConsumer
import com.xunyun.kotlinwc.network.lifecycle.RxLifeCycle
import com.xunyun.kotlinwc.network.response.ResponseTransformer
import com.xunyun.kotlinwc.utils.paging.PagingConfig
import io.reactivex.functions.Consumer

/**
 * @author gl
 * @date 2021/9/10
 * @desc
 */
class ProjectDataSource(
    var lifecycleOwner: LifecycleOwner,
    var infoMsg: MutableLiveData<String>,
    var cid: Int
) : PageKeyedDataSource<Int, Project>() {

    @SuppressLint("CheckResult")
    override fun loadInitial(
        params: LoadInitialParams<Int>,
        callback: LoadInitialCallback<Int, Project>
    ) {
        RetrofitHolder.singleton.getRetrofit()
            .create(MainService::class.java)
            .getProjectData(PagingConfig.FIRST_PAGE, PagingConfig.PAGE_SIZE, cid)
            .compose(ResponseTransformer())
            .compose(RxLifeCycle(lifecycleOwner))
            .subscribe(Consumer {
                it?.let {
                    if (null == it) {
                        infoMsg.value = "加载失败请重新加载"
                        return@Consumer
                    }
                    it.datas?.let { articles ->
                        callback.onResult(
                            articles,
                            null,
                            PagingConfig.FIRST_PAGE + 1
                        )
                    }
                }
            }, object : ErrorConsumer() {
                override fun error(apiException: ApiException?) {
                    infoMsg.value = apiException?.let {
                        it.msg
                    }
                }
            })


    }

    override fun loadBefore(params: LoadParams<Int>, callback: LoadCallback<Int, Project>) {

    }

    @SuppressLint("CheckResult")
    override fun loadAfter(
        params: LoadParams<Int>,
        callback: LoadCallback<Int, Project>
    ) {
        RetrofitHolder.singleton.getRetrofit()
            .create(MainService::class.java)
            .getProjectData(params.key, PagingConfig.PAGE_SIZE, cid)
            .compose(ResponseTransformer())
            .compose(RxLifeCycle(lifecycleOwner))
            .subscribe(Consumer {
                if (params.key == null || it.over) {
                    infoMsg.value = "没有更多数据了"
                    return@Consumer
                }
                it?.let {
                    val nextKey = if (!it.over) params.key + 1 else null;
                    it.datas?.let { articles -> callback.onResult(articles, nextKey) }
                }

            }, object : ErrorConsumer() {
                override fun error(apiException: ApiException?) {
                    infoMsg.value = apiException?.let {
                         it.msg
                    }
                }
            })
    }
}